import React from "react";

import CommonSendFetchAction from "../common/CommonSendFetchAction";
import Constants from "../../common/Constants";

export default class UserUpdateSelfDataAction extends CommonSendFetchAction {
  constructor() {
    super();

    this.moduleType = Constants.MODULE_TYPE.USER;
    this.name = "UserUpdateSelfDataAction";
  }

  finish(data, result, response, callback) {
    console.log("UserUpdateSelfDataAction finish", data);
    if (result) {
      this._dealFinishSucess(data, response, callback);
    } else {
      if (response.status === ResponseConstants.status_unauthorized) {
        this._dealUnauthorized();
      } else {
        this._dealFinishFailed(data, response);
      }
    }
  }

  _dealFinishSucess(data, response, callback) {
    if (this.data.avatar_url) {
      Constants.USER.avatar_url = this.data.avatar_url;
    }
    if (this.data.nick) {
      Constants.USER.nick = this.data.nick;
    }
    if (this.data.email) {
      Constants.USER.email = this.data.email;
    }
    if (this.data.department) {
      Constants.USER.department = this.data.department;
    }
    if (this.data.section) {
      Constants.USER.section = this.data.section;
    }
    if (this.data.gender) {
      Constants.USER.gender = this.data.gender;
    }
    if (this.data.birthday) {
      Constants.USER.birthday = this.data.birthday;
    }
    callback(Constants.USER);
    this.caller.setState({
      statusText: "修改成功",
      updateResult: true
    });
  }
}
